import 'dart:async';

import 'package:check_app/page/SubjectPage.dart';
import 'package:check_app/scoped_model/app_model.dart';
import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'package:check_app/page/HomePage.dart';
import 'package:check_app/page/MyPage.dart';
import 'package:scoped_model/scoped_model.dart';

class Navigete extends StatefulWidget {
  Navigete({Key key}) : super(key: key);

  @override
  _NavigeteState createState() => _NavigeteState();
}

class _NavigeteState extends State<Navigete> {
  // 当前导航页索引
  int _currentIndex = 0;
  // 页面集合
  List<Widget> _pageList = [
    HomePage(),
    SubjectPage(),
    MyPage(),
  ];
  // 底部导航按钮颜色
  final _bottomNavigationColor = Color.fromRGBO(89, 131, 252, 1);
  final _bottomNavigationDefaultColor = Color.fromRGBO(150, 151, 154, 1);

  // 网络状态检测实例
  StreamSubscription<ConnectivityResult> _subscription;

  @override
  void initState() {
    super.initState();
    // 检测当前网络状态
    initConnectivity();
    // 监听网络状态，当网络状态改变时，获取最新的网络状态
    _subscription = Connectivity()
        .onConnectivityChanged
        .listen((ConnectivityResult result) {
      ScopedModel.of<GlobalModel>(context)
          .changeConnectionStatus(result.toString());
    });
  }

  Future<Null> initConnectivity() async {
    String connectionStatus;
    //平台消息可能会失败，因此我们使用Try/Catch PlatformException。
    try {
      connectionStatus = (await Connectivity().checkConnectivity()).toString();
    } catch (e) {
      print(e.toString());
      connectionStatus = '信号检测失败！';
    }

    // 如果在异步平台消息运行时从树中删除了该小部件，
    // 那么我们希望放弃回复，而不是调用setstate来更新我们不存在的外观。
    if (!mounted) {
      return;
    }

    ScopedModel.of<GlobalModel>(context)
        .changeConnectionStatus(connectionStatus);
  }

  @override
  dispose() {
    super.dispose();
    // widget销毁时，取消网络状态的监听
    // _subscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pageList[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (int index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: _currentIndex == 0
                ? Image(
                    image: AssetImage('assets/images/task_select.png'),
                    width: 24,
                    height: 24,
                  )
                : Image(
                    image: AssetImage('assets/images/task.png'),
                    width: 24,
                    height: 24,
                  ),
            title: Text(
              '任务',
              style: TextStyle(
                  color: _currentIndex == 0
                      ? _bottomNavigationColor
                      : _bottomNavigationDefaultColor),
            ),
          ),
          BottomNavigationBarItem(
            icon: _currentIndex == 1
                ? Image(
                    image: AssetImage('assets/images/goods_select.png'),
                    width: 24,
                    height: 24,
                  )
                : Image(
                    image: AssetImage('assets/images/goods.png'),
                    width: 24,
                    height: 24,
                  ),
            title: Text(
              '待检物',
              style: TextStyle(
                  color: _currentIndex == 1
                      ? _bottomNavigationColor
                      : _bottomNavigationDefaultColor),
            ),
          ),
          BottomNavigationBarItem(
            icon: _currentIndex == 2
                ? Image(
                    image: AssetImage('assets/images/my_select.png'),
                    width: 24,
                    height: 24,
                  )
                : Image(
                    image: AssetImage('assets/images/my.png'),
                    width: 24,
                    height: 24),
            title: Text(
              '我的',
              style: TextStyle(
                  color: _currentIndex == 2
                      ? _bottomNavigationColor
                      : _bottomNavigationDefaultColor),
            ),
          ),
        ],
      ),
    );
  }
}
